package com.hcss.sys.menu.service.impl;

import com.hcss.common.base.BaseClass;
import com.hcss.sys.menu.constant.MenuConstants;
import com.hcss.sys.menu.dao.HcSysMenuComplexMapper;
import com.hcss.sys.menu.dao.HcSysMenuMapper;
import com.hcss.sys.menu.model.HcSysMenu;
import com.hcss.sys.menu.model.HcSysMenuExample;
import com.hcss.sys.menu.model.vo.HcSysMenuVO;
import com.hcss.sys.menu.service.MenuService;
import com.hcss.sys.role.dao.HcSysRoleComplexMapper;
import com.hcss.sys.role.model.vo.HcSysRolemenuVO;
import com.hcss.update.model.UpdateSchema;
import com.hcss.util.BeanCopyUtil;
import com.hcss.util.EmptyUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @描述: MenuService服务的实现类
 * @作者: fangtao
 * @创建日期: 2019-01-07 16:56:48
 */
@Service("menuService")
public class MenuServiceImpl implements MenuService {
	
	@Autowired
	private HcSysMenuMapper hcSysmenuMapper;

	@Autowired
	private HcSysMenuComplexMapper hcSysMenuComplexMapper;

	@Autowired
	private HcSysRoleComplexMapper hcSysRoleComplexMapper;


	@Override
	public void insertModel(HcSysMenu model) {
		model.setId(BaseClass.getUUIDWithoutConnector());
		hcSysmenuMapper.insertSelective(model);
	}

	@Override
	public void updateModel(HcSysMenu model, UpdateSchema updateSchema) throws Exception {
		if(EmptyUtil.isHaveEmpty(updateSchema)){
			hcSysMenuComplexMapper.updateByPrimaryKeySelective(model);
		} else {
			HcSysMenu oldModel = hcSysMenuComplexMapper.selectByPrimaryKey(model.getId());
			if (EmptyUtil.isHaveEmpty(oldModel)) {
				throw new NullPointerException("未查询到要修改的数据");
			}
			BeanCopyUtil.copy(model, oldModel, updateSchema.getAllFieldNames());
			hcSysMenuComplexMapper.updateByPrimaryKey(model);
		}
	}

	@Override
	public void updateModelSelective(HcSysMenu model) {
		hcSysMenuComplexMapper.updateByPrimaryKeySelective(model);
	}

	@Override
	public void deleteById(String id) {
		HcSysMenu model = selectByPK(id);
		if (model == null) {
            return;
        }

		Collection<HcSysMenu> sonList = getSysMenuByFatherid(model.getId());
		if (CollectionUtils.isNotEmpty(sonList)) {
			for (HcSysMenu son : sonList) {
				deleteSonMenu(son);
			}
		}
		// 修改数据库
		model.setIsDelete(MenuConstants.IS_DELETE_YES);
		hcSysMenuComplexMapper.updateByPrimaryKeySelective(model);
	}

	/**
	 * 删除子菜单
	 * @param model
	 */
	private void deleteSonMenu(HcSysMenu model) {
		Collection<HcSysMenu> sonList = getSysMenuByFatherid(model.getId());
		if (CollectionUtils.isNotEmpty(sonList)) {
			for (HcSysMenu son : sonList) {
				deleteSonMenu(son);
			}
			return;
		}
		model.setIsDelete(MenuConstants.IS_DELETE_YES);
		hcSysMenuComplexMapper.updateByPrimaryKeySelective(model);
	}
	
	@Override
	public HcSysMenu selectByPK(String id) {
		if (EmptyUtil.isHaveEmpty(id)) {
            return null;
        }
		HcSysMenu menu = hcSysMenuComplexMapper.selectByPrimaryKey(id);
		return menu;
	}

	@Override
	public Collection<HcSysMenu> getSysMenuByFatherid(String fatherid) {
		HcSysMenuExample example = new HcSysMenuExample();
		example.setOrderByClause(" sort");
		example.createCriteria().andFatheridEqualTo(fatherid).andIsDeleteEqualTo(MenuConstants.IS_DELETE_NO);
		return hcSysmenuMapper.selectByExample(example);
	}

	@Override
	public List<HcSysMenu> selectByRole(HcSysRolemenuVO model) {
		return hcSysRoleComplexMapper.selectMenuByRoleid(model.getRoleid());
	}

	@Override
	public List<HcSysMenuVO> selectByUserid(String userid) {
		return hcSysRoleComplexMapper.selectMenuByUserid(userid);
	}

	@Override
	public List<HcSysMenuVO> selectAllByUserid(String userid) {
		return hcSysRoleComplexMapper.selectMenuAllByUserid(userid);
	}

    @Override
    public List<String> selectUserMobileUrl(String userid) {
        return hcSysMenuComplexMapper.selectUserMobileMenu(userid);
    }

}
